package com.mc.read.adminapi.conf;

import com.github.pagehelper.PageHelper;
import com.mc.read.adminapi.filter.DomainFilter;
import com.mc.read.adminapi.filter.PermissionsFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import javax.servlet.MultipartConfigElement;
import java.util.Properties;

//import org.woke.hall.filter.TokenFilter;

/**
 * @Author:LiuHao
 * @Date: Create in 9:07 2018/8/23
 **/
@Configuration
public class conf {
    @Bean
    public PageHelper pageHelper() {
        System.out.println("MyBatisConfiguration.pageHelper()");
        PageHelper pageHelper = new PageHelper();
        Properties p = new Properties();
        p.setProperty("dialect","mysql");
//        p.setProperty("dialect","sqlserver");
//        p.setProperty("offsetAsPageNum", "true");
//        p.setProperty("rowBoundsWithCount", "true");
//        p.setProperty("reasonable", "true");
        pageHelper.setProperties(p);
        return pageHelper;
    }


//    @Bean
//    public ServletServerContainerFactoryBean servletServerContainerFactoryBean() {
//        ServletServerContainerFactoryBean servletServerContainerFactoryBean = new ServletServerContainerFactoryBean();
//
//
//        servletServerContainerFactoryBean.setMaxBinaryMessageBufferSize(20971520);
//        servletServerContainerFactoryBean.setMaxTextMessageBufferSize(20971520);
//        return servletServerContainerFactoryBean;
//    }

    @Bean
    public Filter permissionsFilter(){
        return new PermissionsFilter();
    }
    @Bean
    public Filter domainFilter(){
        return new DomainFilter();
    }

    @Bean
    public FilterRegistrationBean demoFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(permissionsFilter());
        registration.addUrlPatterns("/verify/*");
        registration.addInitParameter("paramName", "paramValue");
        registration.setName("permissionsFilter");
        registration.setOrder(Integer.MAX_VALUE);
        return registration;
    }
    @Bean
    public FilterRegistrationBean demoFilter1() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(domainFilter());
        registration.addUrlPatterns("/*");
        registration.addInitParameter("paramName", "paramValue");
        registration.setName("domainFilter");
        registration.setOrder(Integer.MAX_VALUE-1);
        return registration;
    }
    @Bean
    MultipartConfigElement createMultipartConfigElement()
    {
        MultipartConfigFactory mcf = new MultipartConfigFactory();
        /**
         * 设置最大上传文件的大小，默认是10MB
         */
        mcf.setMaxFileSize("100MB");
        return mcf.createMultipartConfig();
    }
//    @Bean
//    MultipartConfigElement multipartConfigElement() {
//        MultipartConfigFactory factory = new MultipartConfigFactory();
//        factory.setLocation("\\app");
//        return factory.createMultipartConfig();
//    }
}
